package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import server.ForumAPIService;
import server.controller.WebAdapter;

import database.DBHandler;
import database.DatabaseStub;
import forum.Post;

/**
 * Servlet implementation class ViewForumsProcessor
 */
// @WebServlet("/ViewForumsProcessor")
public class ViewForumsProcessor extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewForumsProcessor() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init() throws ServletException {
		super.init();
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected synchronized void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("view forums processor. doGet");
		// get session of the request
		HttpSession session = request.getSession();

		// session.setAttribute("isLoggedIn", new Boolean(true));
		Boolean isLoggedIn = (Boolean) session.getAttribute("isLoggedIn");
		Boolean isModerator = (Boolean) session.getAttribute("isModerator");

		if (isLoggedIn == null || !(isLoggedIn.booleanValue())) {
			request.setAttribute("errorMsg", "Please Log In");
			String addr = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath();

			request.setAttribute("errorLink", addr);
			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/Error.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
			return;
		}
		ForumAPIService db = WebAdapter.getInstance();

		// get parameter do of the request
		String action = request.getParameter("do");

		// get the id of the request
		int id = -1;
		if (request.getParameter("id") != null)
			id = Integer.valueOf(request.getParameter("id"));

		if (action.equals("show")) {
			session.setAttribute("forumID", new Integer(id));
			// redirect to the book list servlet
			response.sendRedirect(request.getContextPath() + "/viewThreads");
			return;
		}
		String username = (String) session.getAttribute("username");
		
		if (action != null && action.equals("check")){
			System.out.println("view forums processor. check");
			int notify = ((WebAdapter) db).getUserNotifies(username);
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			String ans = "<response>";
			//check friend notification
			int res = notify & WebAdapter.FRIEND_NOTIFY;
			if (res > 0) {
				ans  += "<friend>y</friend>\n";
			}
			else
				ans  += "<friend>n</friend>\n";
			// check author notification
			res = notify & WebAdapter.AUTHOR_NOTIFY;
			if (res > 0)
				ans += "<author>y</author>\n";
			else
				ans += "<author>n</author>\n";
			ans += "<viewer>n</viewer>\n";
			ans += "</response>";
			System.out.println(ans);
			response.getWriter().write(ans);
			response.getWriter().flush();
			return;
		}

		if (isModerator == null || !(isModerator.booleanValue())) {
			request.setAttribute("errorMsg",
					"You do not have a suitable permittions to access this page");
			String addr = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath()
					+ "/viewForums";

			request.setAttribute("errorLink", addr);
			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/Error.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
			return;
		}

		// add forum
		if (action != null && action.equals("add")) {
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/addForum.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
			return;

		}
		if (action != null && action.equals("delete")) {

			// delete the forum by id
			db.removeForum(id);

			// redirect to the book list servlet
			response.sendRedirect(request.getContextPath() + "/viewForums");
			return;

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected synchronized void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// process add forum request
		System.out.println("view forums processor. doPost");
		ForumAPIService db = WebAdapter.getInstance();

		String subject = request.getParameter("subject");
		String uName = (String)request.getSession().getAttribute("username");

		int res = db.addForum(subject, uName);
		if (res > 0)
			response.sendRedirect(request.getContextPath() + "/viewForums");
		else {
			request.setAttribute("errorMsg", "Adding forum failed");
			String addr = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath()
					+ "/viewForums";

			request.setAttribute("errorLink", addr);
			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/Error.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
			return;
		}

	}

}
